//触摸屏获取触摸坐标
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/input.h>
#include<sys/mman.h>

struct input_event ts_buf;
int x,y;
int ts_fd;
int color = 0xFFFF00;   //笔触颜色信息
int color1 = 0x000000;  //设置黑色
int *p = NULL;          //进度条屏幕信息指针

//图片显示
void showbmp(char *picname)
{
    int fd;
    fd = open(picname, O_RDONLY);
    if (-1 == fd)
    {
        perror("open() fd failed!\n");
        return 0;
    }
    else
    {
        printf("open() fd success\n");
    }

    //解析头部数据
    char head[54] = {0};
    read(fd, &head, sizeof(head));
    int gao = *((int *)&head[18]);
    int kuan = *((int *)&head[22]);

    //根据宽和高定义存放的像素数据的缓存区
    char buf[gao * kuan * 3];
    char buf32[gao * kuan * 4];
    char buffz[gao * kuan * 4];

    //文件偏移将头部数据覆盖
    lseek(fd,54,SEEK_SET);

    //把图像的像素数据读入buf中
    read(fd, &buf, sizeof(buf));

    //开始转码
    int x = 0, i = 0, y = 0;
    for (i = 0; i < gao * kuan; i++)
    {
        buf32[0 + i * 4] = buf[0 + i * 3];
        buf32[1 + i * 4] = buf[1 + i * 3];
        buf32[2 + i * 4] = buf[2 + i * 3];
        buf32[3 + i * 4] = 0;
    }

    //翻转算法
    for (y = 0; y < 480; y++)
    {
        for (x = 0; x < 800 * 4; x++)
        {
            buffz[800 * 4 * (479 - y) + x] = buf32[800 * 4 * y + x];
        }
    }

    //打开LCD屏幕
    int lcd_fd;
    lcd_fd = open("/dev/fb0",O_RDWR);
    if (-1 == lcd_fd)
    {
        perror("open() lcd failed!\n");
        return 0;
    }
    else
    {
        printf("open() lcd success\n");
    }

    //写入像素
    write(lcd_fd,buffz,sizeof(buffz));

    //关闭文件接口
    close(lcd_fd);
    close(fd);
}

int ts_open(void)
{
	//打开触摸屏的驱动
	
	ts_fd = open("/dev/input/event0", O_RDWR);
	if(-1 == ts_fd)
	{
		perror("open ts failed!\n");
		return -1;
	}
}

int ts_get(int *x, int *y)
{
	//读取触摸屏的数据
	bzero(&ts_buf, sizeof(ts_buf));
	while(1)
	{
		read(ts_fd, &ts_buf, sizeof(ts_buf));
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_X)
		{
			*x = ts_buf.value*800/1024;//1024
		}
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_Y)
		{
			*y = ts_buf.value*480/600;//600
		}
		if(ts_buf.type == EV_KEY && ts_buf.code == BTN_TOUCH && ts_buf.value == 0)
		{
            //由于未能解决BUG，项目暂停
			printf("(%d,%d)\n",x,y);
		}
	}
	
}

void qinchu(int x,int y,int x1,int y1){

    int lcd_fd = open("/dev/fb0",O_RDWR);
    if (-1 == lcd_fd)
    {
        perror("open() lcd failed\n");
        return -1;
    }
    else
    {
        printf("open() lcd success\n");
    }

    p = mmap(NULL,800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED,lcd_fd,0);

    if (NULL == p)
    {
        perror("mmap() failed\n");
        return 0;
    }

    //将区域显示为黑色
    for (size_t i = x; i < x1; i++)
	{
		for(size_t r = y; r <= y1;r++){
		    *(p+i+r*800) = color1;
	    }
	}

    close(lcd_fd);
}

void setxy(int x,int y){
    int lcd_fd = open("/dev/fb0",O_RDWR);
    if (-1 == lcd_fd)
    {
        perror("open() lcd failed\n");
        return -1;
    }
    else
    {
        printf("open() lcd success\n");
    }

    p = mmap(NULL,800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED,lcd_fd,0);

    if (NULL == p)
    {
        perror("mmap() failed\n");
        return 0;
    }

	*(p+x+y*800) = color;

    close(lcd_fd);
}

void bichu(){

    showbmp("./pic/9.bmp");
    int qinchu_flag = 1;
    int max_x = 0,max_y = 0;
    int min_x = 0,min_y = 0;
    while(1)
	{
		printf("点击获取坐标\n");
		ts_get(&x,&y);  //x,y为全局变量
		printf("(%d,%d)\n",x,y);
        //点位更新
        if(x > max_x){
            max_x = x;
        }
        if(x < min_x){
            min_x = x;
        }
        if(y > max_y){
            if(y >= 400){
                max_y = 400;
            }else{
                max_y = y;
            }
        }
        if(y < min_y){
            min_y = y;
        }
		//点击清除笔触
        if(x >= 0 && x <= 170 && y >= 397 && y <= 477){
            qinchu(min_x,min_y,max_x,max_y);
        }

        //留下笔记
        setxy(x,y);

        //点击返回
        if(x >= 547 && x <= 770 && y >= 313 && y <= 400){
            showbmp("./pic/5.bmp");
            break;
        }
	}
}

int main()
{
	ts_open();
    //bichu();
	while(1)
	{
		printf("点击获取坐标\n");
		ts_get(&x,&y);
		printf("(%d,%d)\n",x,y);
		//if(x)
	}
	close(ts_fd);
	return 0;
}